package com.zrfintech.api.config;

import com.zrfintech.common.concurrent.SparrowThreadPoolTaskExecutor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.ThreadPoolExecutor;

@Configuration
public class ThreadPoolConfig {

    @Bean("apiTimeOutControlPool")
    public SparrowThreadPoolTaskExecutor apiTimeOutControlTaskExecutor() {
        SparrowThreadPoolTaskExecutor taskExecutor = new SparrowThreadPoolTaskExecutor();
        taskExecutor.setPoolName("apiTimeOutControl");
        taskExecutor.setCorePoolSize(16);
        taskExecutor.setMaxPoolSize(16);
        taskExecutor.setQueueCapacity(1000);
        taskExecutor.setThreadNamePrefix("custom-thread-apiTimeOutControl");
        // 线程空闲时间（超过此时间的空闲线程会被回收）
        taskExecutor.setKeepAliveSeconds(60);
        // 拒绝策略（当队列和最大线程数都满时的处理方式）
        taskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        // 初始化线程池
        taskExecutor.initialize();
        return taskExecutor;
    }


    @Bean("saveReqContextPool")
    public SparrowThreadPoolTaskExecutor saveReqContextTaskExecutor() {
        SparrowThreadPoolTaskExecutor taskExecutor = new SparrowThreadPoolTaskExecutor();
        taskExecutor.setPoolName("saveReqContext");
        taskExecutor.setCorePoolSize(16);
        taskExecutor.setMaxPoolSize(16);
        taskExecutor.setQueueCapacity(1000);
        taskExecutor.setThreadNamePrefix("custom-thread-saveReqContext");
        // 线程空闲时间（超过此时间的空闲线程会被回收）
        taskExecutor.setKeepAliveSeconds(60);
        // 拒绝策略（当队列和最大线程数都满时的处理方式）
        taskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        // 初始化线程池
        taskExecutor.initialize();
        return taskExecutor;
    }
}
